package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.awt.datatransfer.FlavorEvent;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Override
    @Transactional//事务注解
    public void save(DishDTO dishDTO) {
        log.info("save dishDTO: {}", dishDTO);
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insertDish(dish);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dish.getId());
            }
            dishFlavorMapper.insertFlavors(flavors);
        }
    }

    @Override
    public DishVO getDishById(Long id) {
     Dish dish= dishMapper.getDishById(id);
     DishVO dishVO = new DishVO();
     List<DishFlavor> dishFlavor= dishFlavorMapper.getDishFlavorById(id);
     BeanUtils.copyProperties(dish, dishVO);
     dishVO.setFlavors(dishFlavor);
     return dishVO;
    }

    @Override
    public PageResult page(DishPageQueryDTO pageQueryDTO) {
        PageHelper.startPage(pageQueryDTO.getPage(),pageQueryDTO.getPageSize());
        //下一条sql进行分页，自动加入limit关键字分页
        Page<DishVO> page = dishMapper.DishpageQuery(pageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Long getStatusById(Long i) {
        return dishMapper.getStatusById(i);
    }
    @Override
    @Transactional
    public void deleteBath(List<Long> ids) {
        //判断是否有菜品在起售中
        for (int i = 0; i < ids.size(); i++) {
            if(dishMapper.getStatusById(ids.get(i))==1){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //判断该菜品是否有套餐
        for (int i = 0; i <ids.size(); i++) {
            if (dishMapper.getsetmealByDishById(ids.get(i))>0){
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        }
        dishMapper.deleteDishByIds(ids);
        dishFlavorMapper.deleteDishFlavorByIds(ids);
    }

    @Override
    @Transactional
    public Result update(DishVO dishVO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishVO, dish);
        //修改菜品
        dishMapper.updateDish(dish);
            //获得菜品id，根据菜品id删除原有的口味
            List<Long> flavorIds =new ArrayList<>();
            flavorIds.add(dishVO.getId());
            //删除原有的口味
            dishFlavorMapper.deleteDishFlavorByIds(flavorIds);
            List<DishFlavor> flavors = dishVO.getFlavors();
        if (flavors.size() > 0){
            for (int i = 0; i < flavors.size(); i++) {
                flavors.get(i).setDishId(dishVO.getId());
            }
                dishFlavorMapper.insertFlavors(flavors);
            }
        return Result.success();
    }
    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        dishMapper.updateStatus(id,status);
    }

    @Override
    public List<Dish> list(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }
}